import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:nyh_common_package/common/widgets/url_link_page.dart';
import 'package:nyh_motorcade_app/config/config.dart';
import 'package:nyh_motorcade_app/routes/routes.dart';
import 'package:nyh_motorcade_app/styles/colors.dart';
import 'login_controller.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class LoginPage extends StatelessWidget {
  final controller = Get.put(LoginController());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      resizeToAvoidBottomInset: false,
      body: Stack(
        alignment: AlignmentDirectional.center,
        children: [
          buildBackgroundWidget(),
          buildLogoWidget(),
          buildLoginWidget(),
          buildBottomWidget(),
        ],
      ),
    );
  }

  Widget buildBackgroundWidget() {
    return Positioned(
      left: 0,
      right: 0,
      top: 0,
      bottom: 0,
      child: Image.asset(
        "images/bg_login.png",
        fit: BoxFit.fill,
      ),
    );
  }

  Widget buildLogoWidget() {
    return Positioned(
      top: ScreenUtil().statusBarHeight + 34.w,
      child: GestureDetector(
        onLongPress: () {
          controller.setLogoLongPress(true);
        },
        onDoubleTap: () {
          if (controller.isLogoLongPress) {
            controller.setLogoLongPress(false);
            Get.toNamed(Routes.about);
          }
        },
        child: Image.asset(
          "images/ic_login_logo.png",
        ),
      ),
    );
  }

  Widget buildLoginWidget() {
    return Positioned(
      left: 0,
      right: 0,
      top: ScreenUtil().statusBarHeight + 108.w,
      bottom: ScreenUtil().statusBarHeight + 108.w,
      child: Container(
        margin: EdgeInsets.symmetric(horizontal: 16.w),
        padding: EdgeInsets.all(16.w),
        decoration: BoxDecoration(
          color: Colours.color_white,
          borderRadius: BorderRadius.circular(18.w),
          boxShadow: [
            BoxShadow(
              color: Colours.color_0f112e_95,
              offset: Offset(0.0, 2.w),
              blurRadius: 5.w,
            ),
          ],
        ),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            SizedBox(
              height: 12.w,
            ),
            Container(
              alignment: Alignment.center,
              child: Text(
                "欢迎使用能源汇车队端",
                style: TextStyle(
                  color: Colours.color_text_theme,
                  fontSize: 20.sp,
                  fontWeight: FontWeight.w600,
                ),
              ),
            ),
            SizedBox(
              height: 44.w,
            ),
            Container(
              margin: EdgeInsets.symmetric(horizontal: 8.w),
              decoration: BoxDecoration(
                boxShadow: [
                  BoxShadow(
                    color: Colours.color_theme_60,
                    offset: Offset(0.0, 6.w),
                    blurRadius: 15.w,
                  ),
                ],
              ),
              child: GetBuilder<LoginController>(
                builder: (controller) {
                  return TextButton(
                    onPressed: () {
                      controller.oneKeyLoginAuthOrPhoneLogin();
                    },
                    style: ButtonStyle(
                      backgroundColor: MaterialStateProperty.resolveWith((states) {
                        return Colours.color_theme;
                      }),
                      overlayColor: MaterialStateProperty.resolveWith((states) {
                        return Colours.color_theme_deep;
                      }),
                      shape: MaterialStateProperty.all(
                        RoundedRectangleBorder(
                          borderRadius: BorderRadius.circular(28.w),
                        ),
                      ),
                      minimumSize: MaterialStateProperty.all(Size(double.infinity, 48.w)),
                    ),
                    child: Text(
                      (controller.isVerifyInitSuccess && controller.isVerifyEnable) ? "本机号码一键登录" : "手机验证码登录",
                      style: TextStyle(color: Colours.color_white, fontSize: 16.sp),
                    ),
                  );
                },
              ),
            ),
            SizedBox(
              height: 21.w,
            ),
            Container(
              margin: EdgeInsets.only(left: 8.w),
              child: TextButton.icon(
                onPressed: () {
                  controller.changePrivacyPolicy();
                },
                icon: GetBuilder<LoginController>(
                  builder: (controller) {
                    return Image.asset(controller.isAgreePrivacyPolicy ? "images/ic_selected.png" : "images/ic_unselect.png");
                  },
                ),
                label: RichText(
                  text: TextSpan(
                    children: [
                      TextSpan(
                        text: "已阅读并同意",
                        style: TextStyle(color: Colours.color_9fa0ab, fontSize: 12.sp),
                      ),
                      TextSpan(
                        text: "《用户隐私协议》",
                        style: TextStyle(color: Colours.color_text_theme, fontSize: 12.sp),
                        recognizer: TapGestureRecognizer()
                          ..onTap = () {
                            Get.toNamed(Routes.privacyPolicy);
                          },
                      ),
                    ],
                  ),
                ),
                style: ButtonStyle(
                  tapTargetSize: MaterialTapTargetSize.shrinkWrap,
                  minimumSize: MaterialStateProperty.all(Size(0, 0)),
                  padding: MaterialStateProperty.all(EdgeInsets.all(0)),
                ),
              ),
            ),
            Expanded(child: Container()),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              children: [
                SizedBox(
                  width: 16.w,
                ),
                // Material(
                //   child: InkWell(
                //     onTap: () {
                //       Get.offNamed(Routes.main);
                //     },
                //     child: Padding(
                //       padding: EdgeInsets.symmetric(vertical: 10.w, horizontal: 30.w),
                //       child: Column(
                //         children: [
                //           Image.asset(
                //             "images/ic_wechat_login.png",
                //             fit: BoxFit.cover,
                //             height: 36.w,
                //             width: 36.w,
                //           ),
                //           SizedBox(
                //             height: 8.w,
                //           ),
                //           Text(
                //             "微信登录",
                //             style: TextStyle(color: Colours.color_9fa0ab, fontSize: 12.sp),
                //           )
                //         ],
                //       ),
                //     ),
                //   ),
                // ),
                GetBuilder<LoginController>(
                  builder: (controller) {
                    return Visibility(
                      visible: (controller.isVerifyInitSuccess && controller.isVerifyEnable),
                      child: Material(
                        child: InkWell(
                          onTap: () {
                            Get.toNamed(Routes.phoneLogin);
                          },
                          child: Padding(
                            padding: EdgeInsets.symmetric(vertical: 10.w, horizontal: 30.w),
                            child: Column(
                              children: [
                                Image.asset(
                                  "images/ic_phone_login.png",
                                  fit: BoxFit.cover,
                                  height: 36.w,
                                  width: 36.w,
                                ),
                                SizedBox(
                                  height: 8.w,
                                ),
                                Text(
                                  "手机验证码登录",
                                  style: TextStyle(color: Colours.color_9fa0ab, fontSize: 12.sp),
                                )
                              ],
                            ),
                          ),
                        ),
                      ),
                    );
                  },
                ),
                SizedBox(
                  width: 16.w,
                ),
              ],
            ),
            SizedBox(
              height: 20.w,
            ),
            Align(
              alignment: Alignment.centerRight,
              child: TextButton.icon(
                onPressed: () {
                  //Get.toNamed(Routes.cooperate);
                  UrlLinkPage.launchUrlPage(Get.context!, Config.getCooperateUrl());
                },
                icon: Image.asset("images/ic_cooperate.png"),
                label: Text(
                  "我要合作",
                  style: TextStyle(
                    color: Colours.color_9fa0ab,
                    fontSize: 12.sp,
                  ),
                ),
                style: ButtonStyle(
                  tapTargetSize: MaterialTapTargetSize.shrinkWrap,
                  minimumSize: MaterialStateProperty.all(Size(0, 0)),
                  padding: MaterialStateProperty.all(EdgeInsets.all(0)),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget buildBottomWidget() {
    return Positioned(
      bottom: ScreenUtil().bottomBarHeight + 20.w,
      child: Image.asset(
        "images/ic_login_text.png",
      ),
    );
  }
}
